import { fetchSwaggerConfig, updateSwaggerConfig } from './service';

const Model = {
  namespace: 'swaggerConfig',
  state: {
    config: '',
  },
  effects: {
    *fetch({ payload }, { call, put }) {
      // 获取回调函数
      const callback = payload.callback;
      delete payload.callback;

      const response = yield call(fetchSwaggerConfig, payload);
      callback && callback();

      yield put({
        type: 'save',
        payload: response,
      });
    },

    *update({ payload }, { call, put }) {
      // 获取回调函数
      const callback = payload.callback;
      delete payload.callback;

      const response = yield call(updateSwaggerConfig, payload);
      callback && callback();
      yield put({
        type: 'save',
        payload,
      });
    },
  },
  reducers: {
    save(state, action) {
      return { ...action.payload };
    },
  },
};
export default Model;
